<?php
include_once('prepend.inc.php');
include_once('PeopleWidget.php');
include_once('Panel/pnlHeader.php');
class MyLibrary extends QForm
{
	protected $txtSearchTerm;
	protected $rdbSearchCategory;
	protected $rdbSearchOptions;
	protected $btnSearch;
	protected $dtrMyLibrary;
	protected $lnkAddAssets;
	protected $lnkHome;
	protected $objWaitIcon;
	protected $objConfirm;
	protected $dlgPeopleBox;
	protected $pnlHeader;


	protected function Form_Run()
	{
		QApplication::CheckRemoteAdmin();
		QApplication::$LoadedPage = 'My Library';
	}

	protected function Form_Create()
	{
		QApplication::$LoadedPage = 'My Library';
		$this->objWaitIcon = new QWaitIcon($this);
		$this->txtSearchTerm_Create();
		$this->btnSearch_Create();
		$this->rdbSearchCategory_Create();
		$this->rdbSearchOptions_Create();
		$this->dtrMyLibrary_Create();
		$this->lnkAddAssets_Create();
		$this->lnkHome_Create();
		$this->objConfirm_Create();
		$this->dlgPeopleBox_Create();
		$this->pnlHeader_Create();
		if(isset($_POST['txtSearchAssets']))
		{
			$this->txtSearchTerm = $_POST['txtSearchAssets'];
		}
		
	}

	protected function txtSearchTerm_Create()
	{
		$this->txtSearchTerm = new QTextBox($this);

	}

	protected function rdbSearchCategory_Create()
	{
		$this->rdbSearchCategory = new QRadioButtonList($this);
		$this->rdbSearchCategory->AddItem(new QListItem('Books','B',true));
		$this->rdbSearchCategory->AddItem(new QListItem('Movies','M',false));
		$this->rdbSearchCategory->AddItem(new QListItem('Music','C',false));
		$this->rdbSearchCategory->RepeatDirection = QRepeatDirection::Horizontal;
		$this->rdbSearchCategory->RepeatColumns = 3;
		$this->rdbSearchCategory->AddAction(new QChangeEvent,new QServerAction('rdbSearchCategory_Change'));
	}
	
	protected function rdbSearchOptions_Create()
	{
		$this->rdbSearchOptions = new QRadioButtonList($this);
		$this->rdbSearchOptions->AddItem(new QListItem('Search My Library','L',true));
		$this->rdbSearchOptions->AddItem(new QListItem('Search and Add from Amazon','A',false));
		$this->rdbSearchOptions->RepeatDirection = QRepeatDirection::Horizontal;
		$this->rdbSearchOptions->RepeatColumns = 2;
		$this->rdbSearchOptions->AddAction(new QChangeEvent,new QAjaxAction('rdbSearchOptions_Change'));
	}
	
	protected function btnSearch_Create()
	{
		$this->btnSearch = new QButton($this);
		$this->btnSearch->Text = 'Search My Library';
		$this->btnSearch->AddAction(new QClickEvent(),new QServerAction('btnSearch_Click',$this->objWaitIcon));
	}

	protected function btnSearch_Click()
	{
		$this->dtrMyLibrary_Bind();
	}
	
	protected function dtrMyLibrary_Create()
	{
		$this->dtrMyLibrary = new QDataRepeater($this);
		$this->dtrMyLibrary->Paginator = new QPaginator($this);
		$this->dtrMyLibrary->ItemsPerPage = 4;
		$this->dtrMyLibrary->UseAjax = false;
		$this->dtrMyLibrary->Template = 'libraryresult.tpl.php';
		$this->dtrMyLibrary->SetDataBinder('dtrMyLibrary_Bind');
	}

	protected function dtrMyLibrary_Bind()
	{
		switch($this->rdbSearchCategory->SelectedValue)
		{
			case 'B':
				$productgroup = 'Book';
				break;
			case 'M':
				$productgroup = 'DVD';
				break;
			case 'C':
				$productgroup = 'Music';
				break;
		}
		if($_SESSION['User'] <> '')
			$this->GetAssetsData($productgroup);
	}
	
	protected function lnkAddAssets_Create()
	{
		$this->lnkAddAssets = new QLinkButton($this);
		//$this->lnkAddAssets->ImageUrl = '../assets/images/32x32/process_add.png';
		$this->lnkAddAssets->Text = 'Add an Asset';
		$this->lnkAddAssets->ToolTip = 'Add an Asset';
		$this->lnkAddAssets->AddAction(new QClickEvent(),new QServerAction('AddAsset_Click'));
	}

	protected function lnkHome_Create()
	{
		$this->lnkHome = new QLinkButton($this);
		$this->lnkHome->Text = 'Home';
		$this->lnkHome->ToolTip = 'Home';
		$this->lnkHome->AddAction(new QClickEvent(),new QServerAction('Home_Click'));
	}

	protected function AddAsset_Click()
	{
		QApplication::Redirect('AmazonSearch.php');
	}

	protected function Home_Click()
	{
		QApplication::Redirect('index.php');
	}

	protected function objConfirm_Create()
	{
		$this->objConfirm = new QConfirmAction('');

	}
	
	private function GetAssetsData($productgroup)
	{
		$objClauses = array();
		array_push($objClauses, QQ::OrderBy(QQN::Myassets()->Title));
		if ($objClause = $this->dtrMyLibrary->LimitClause)
		{
			array_push($objClauses, $this->dtrMyLibrary->LimitClause);
		}
		if($this->txtSearchTerm->Text == "")
		{
			$condition = QQ::AndCondition(QQ::Equal(QQN::Myassets()->ProductGroup,$productgroup),
										  QQ::Equal(QQN::Myassets()->Owner,$_SESSION['User'])
										 );
			$arrResult = Myassets::QueryArray($condition,$objClauses);
			$this->dtrMyLibrary->TotalItemCount = Myassets::QueryCount($condition);
			$this->dtrMyLibrary->DataSource = $arrResult;
		}
		else
		{
			$objCondition =QQ::AndCondition
				(
					QQ::OrCondition(
					QQ::Like(QQN::Myassets()->Title,'%'. $this->txtSearchTerm->Text . '%'),
					QQ::Like(QQN::Myassets()->Author,'%'. $this->txtSearchTerm->Text . '%'),
					QQ::Like(QQN::Myassets()->Artist,'%'. $this->txtSearchTerm->Text . '%'),
					QQ::Like(QQN::Myassets()->Actor,'%'. $this->txtSearchTerm->Text . '%'),
					QQ::Like(QQN::Myassets()->Director,'%'. $this->txtSearchTerm->Text . '%'),
					QQ::Like(QQN::Myassets()->Publisher,'%'. $this->txtSearchTerm->Text . '%'),
					QQ::Like(QQN::Myassets()->Label,'%'. $this->txtSearchTerm->Text . '%'),
					QQ::Like(QQN::Myassets()->Isbn,'%'. $this->txtSearchTerm->Text . '%')
					),
					QQ::Equal(QQN::Myassets()->ProductGroup,$productgroup),
					QQ::Equal(QQN::Myassets()->Owner,$_SESSION['User'])
				); 
			$objDbResult = Myassets::QueryArray($objCondition, $objClauses);
			$myassets = $objDbResult;
			$this->dtrMyLibrary->TotalItemCount = Myassets::QueryCount($objCondition);
			if($this->dtrMyLibrary->TotalItemCount > 0)
			{
				$this->dtrMyLibrary->DataSource = $myassets;
			}
			else
			{
				QApplicationBase::DisplayAlert('No ' . $productgroup . 's exists for the searched term');
			}
		}
	}
	
	protected function rdbSearchCategory_Change()
	{
		$this->dtrMyLibrary_Bind();
	}
	
	protected function rdbSearchOptions_Change()
	{
		switch($this->rdbSearchOptions->SelectedValue)
		{
			case 'L':
				$this->btnSearch->Text = 'Search My Library';
				break;
			case 'A':
				$this->btnSearch->Text = 'Search Amazon';
				break;
		}
	}
	
	protected function dlgPeopleBox_Create()
	{
		$this->dlgPeopleBox = new PeopleWidget('',$this);
        $this->dlgPeopleBox->Width = '500px';
        $this->dlgPeopleBox->Height = '500px';
        $this->dlgPeopleBox->Overflow = QOverflow::Scroll;
        $this->dlgPeopleBox->Padding = '10px';
        $this->dlgPeopleBox->FontSize = '12px';
		$this->dlgPeopleBox->Visible = false;
	}
	
	protected function LendThis_Click()
	{
		$this->dlgPeopleBox->ShowDialogBox();
	}
	
	protected function pnlHeader_Create()
	{
		$this->pnlHeader = new pnlHeader($this, 'CloseEditPane');
	}

}

MyLibrary::Run('MyLibrary');
?>